import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

class Practice {
    //判定是否互为字符重排
    public boolean CheckPermutation(String s1, String s2) {
        //两个字符串长度不相等一定是不能重排
        if (s1.length() != s2.length()) {
            return false;
        }
        //将两个字符串的字符放到两个数组中
        char[] str1 = s1.toCharArray();
        char[] str2 = s2.toCharArray();
        //分别排序
        Arrays.sort(str1);
        Arrays.sort(str2);
        //排序好的是否相等
        return Arrays.equals(str1, str2);
    }

    //最后一块石头的重量
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
        for (int stone : stones) {
            pq.offer(stone);
        }

        while (pq.size() > 1) {
            int a = pq.poll();
            int b = pq.poll();
            if (a > b) {
                pq.offer(a - b);
            }
        }
        return pq.isEmpty() ? 0 : pq.poll();

    }
}

public class HomeWorkDay7 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        //输入两个字符串
        String s1 = input.next();
        String s2 = input.next();

        //输入数组长度
        int n = input.nextInt();
        int[] nums = new int[n];
        System.out.println("输入数组元素：");
        for (int i = 0; i < nums.length; i++) {
            nums[i] = input.nextInt();
        }

        Practice practice = new Practice();
        System.out.println(practice.CheckPermutation(s1,s2));
        System.out.println(practice.lastStoneWeight(nums));
    }
}
